package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private ImageView compressSizeImageView;
    private String imgUrl = "http://up.wawa.fm/15,01a5fa8b198543";
    private int screenWidth = 0 ;
    private int screenHeight = 0 ;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,screenWidth - dp2px(32),dp2px(240),true);
//                    Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,150,110,true);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap1.compress(Bitmap.CompressFormat.PNG,100,baos);
                    byte[] bytes = baos.toByteArray();
                    Glide.with(MainActivity.this).load(bytes).into(compressSizeImageView);
                    break;
                default:
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getScreenParams();
    }

    private void getScreenParams() {
        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;
        screenHeight = outMetrics.heightPixels;

    }
    private int dp2px(int value){
        float scale = getResources().getDisplayMetrics().density;
        return (int) (scale * value + 0.5f);
    }
    private void initView() {
        compressSizeImageView = findViewById(R.id.comress_image_size);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Bitmap bitmap = getUrlBitmap(imgUrl);
                    handler.obtainMessage(1,bitmap).sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private Bitmap getUrlBitmap(String imgUrl) throws IOException {
        URL url = new URL(imgUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setReadTimeout(5000);
        if (conn.getResponseCode() == 200){
            InputStream is = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            return bitmap;
        }
        return null;
    }
}
